package com.runchinaup.textapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.runchinaup.textapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liber on 2018/3/8.
 */

public class MyLine extends View {
    public MyLine(Context context) {
        super(context);
        init(context);
    }

    public MyLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private Paint paint;
    private float marginTop;
    private float marginLeft;
    private float marginRight;
    private float marginBottom;
    List<Point> list;

    LinearGradient lg = null;
    private static final int startColor = 0x8045CFAC;
    private static final int endColor = 0x4045CFAC;

    private void init(Context context) {

        paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
        paint.setColor(0xff000000);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(context.getResources().getDimension(R.dimen.x8));

        marginTop = context.getResources().getDimension(R.dimen.x15);
        marginLeft = context.getResources().getDimension(R.dimen.x18);
        marginRight = context.getResources().getDimension(R.dimen.x5);
        marginBottom = context.getResources().getDimension(R.dimen.x15);

        lg = new LinearGradient(50, 50, 50, height - 50,
                startColor, endColor, Shader.TileMode.CLAMP);

        Log.e("init", "2222222222222222222222");


    }

    //view的长和宽
    private float width = 0;
    private float height = 0;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();

        Log.e("onMeasure", "11111111111111111");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(tranlateX, 0);

        list = new ArrayList<>();
        list.add(new Point(0, 37));
        list.add(new Point(12, 50));
        list.add(new Point(25, 25));
        list.add(new Point(37, 37));
        list.add(new Point(50, 100));
        list.add(new Point(62, 75));
        list.add(new Point(75, 62));
        list.add(new Point(87, 12));
        list.add(new Point(100, 87));

        //绘制坐标线和标示坐标
        drawPath(canvas);
        canvas.restore();
        drawAxis(canvas);
    }

    private void drawPath(Canvas canvas) {

        Paint drawLine = new Paint();
        drawLine.setStyle(Paint.Style.FILL);
        drawLine.setColor(Color.GREEN);
        drawLine.setStrokeWidth(getResources().getDimension(R.dimen.x2));

        drawLine.setShader(lg);
        Path path = new Path();
        float count = 8;
        float max = 100;
        float add = max / count;
        float widthPercent = ((width - marginLeft - marginRight) / max);
        float heightPercent = ((width - marginTop - marginBottom) / max);

        path.moveTo(marginLeft, height - marginBottom);
        for (int i = 0; i < list.size(); i++) {
            path.lineTo(list.get(i).x * widthPercent + marginLeft,
                    height - marginBottom - list.get(i).y * heightPercent);
        }
        path.lineTo(width - marginRight, height - marginBottom);
        path.close();
        canvas.drawPath(path, drawLine);

        //标点
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(0xffaabbcc);


        for (int i = 0; i < list.size(); i++) {
            canvas.drawCircle(list.get(i).x * widthPercent + marginLeft,
                    height - marginBottom - list.get(i).y * heightPercent,
                    getResources().getDimension(R.dimen.x2), paint);
        }


    }

    private void drawAxis(Canvas canvas) {

        float count = 8;
        float max = 100;
        float add = max / count;

        //X轴增量
        float xAdd = (width - marginLeft - marginRight) / count;
        //Y轴增量
        float yAdd = (height - marginTop - marginBottom) / count;

        paint.setShader(null);
        paint.setColor(0xffffffff);
        Rect rect = new Rect(0, 0, (int) marginLeft, (int) height);
        canvas.drawRect(rect, paint);


        paint.setColor(0xff000000);
        for (int i = 0; i <= count; i++) {
            //X轴
            canvas.drawLine(marginLeft, height - marginBottom - yAdd * i,
                    width - marginRight, height - marginBottom - yAdd * i, paint);
            //Y轴
            canvas.drawLine(marginLeft + xAdd * i, height - marginBottom,
                    marginLeft + xAdd * i, marginTop, paint);
        }

        //坐标轴
        for (int i = 0; i < count; i++) {
            canvas.drawText(add * i + "", xAdd * i + marginLeft,
                    height - marginBottom / 4, paint);
            canvas.drawText(add * i + "", marginLeft / 2,
                    height - marginBottom - yAdd * i + marginBottom / 5, paint);
        }

//        //画折线
//        Paint drawLine = new Paint();
//        drawLine.setStyle(Paint.Style.STROKE);
//        drawLine.setColor(Color.GREEN);
//        drawLine.setStrokeWidth(getResources().getDimension(R.dimen.x2));
//        drawLine.setShader(lg);
//        for (int i = 0; i < list.size(); i++) {
//            if (i == list.size() - 1)
//                break;
//            canvas.drawLine(list.get(i).x * widthPercent + marginLeft,
//                    height - marginBottom - list.get(i).y * heightPercent,
//                    list.get(i + 1).x * widthPercent + marginLeft,
//                    height - marginBottom - list.get(i + 1).y * heightPercent, drawLine
//            );
//        }


    }

    private float downX = 0;
    private float moveX = 0;
    private float tranlateX = 0, lastX = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getX();
                tranlateX = lastX + (moveX - downX);
                postInvalidateDelayed(40);
                break;
            case MotionEvent.ACTION_UP:
                if (tranlateX > marginLeft) {
                    tranlateX = 0;
                }
                lastX = tranlateX;
                invalidate();
                break;
        }
        return true;
    }

}
